home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / ieee754 / dbl2mpn.c < prev    next >
C/C++ Source or Header  |  1994-01-09  |  3KB  |  76 lines

  1. /* Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include "gmp.h"
  20. #include "gmp-impl.h"
  21. #include "ieee754.h"
  22. #include <stdlib.h>
  23.  
  24. /* Convert a `double' in IEEE754 standard double-precision format to a
  25.    multi-precision integer representing the significand scaled up by its
  26.    number of bits (52 for double) and an integral power of two (MPN frexp). */
  27.  
  28. mp_size_t
  29. __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
  30.               int *expt, int *is_neg,
  31.               double value)
  32. {
  33.   union ieee754_double u;
  34.   u.d = value;
  35.  
  36.   *is_neg = u.ieee.negative;
  37.   *expt = (int) u.ieee.exponent - 1022;
  38.  
  39. #if BITS_PER_MP_LIMB == 32
  40.   res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction.  */
  41.   res_ptr[1] = u.ieee.mantissa0; /* High-order 20 bits.  */
  42.   #define N 2
  43.   #define IMPL1 1 << 20
  44. #elif BITS_PER_MP_LIMB == 64
  45.   /* Hopefully the compiler will combine the two bitfield extracts
  46.      and this composition into just the original quadword extract.  */
  47.   res_ptr[0] = (u.ieee.mantissa0 << 20) | u.ieee.mantissa1;
  48.   #define N 1
  49. #else
  50.   #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
  51. #endif
  52.  
  53.   if (u.ieee.exponent == 0)
  54.     {
  55.       /* A biased exponent of zero is a special case.
  56.      Either it is a zero or it is a denormal number.  */
  57.       if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2.  */
  58.     /* It's zero.  Just one limb records that.  */
  59.     return 1;
  60.  
  61.       /* It is a denormal number, meaning it has no implicit leading
  62.      one bit, and its exponent is in fact the format minimum.  */
  63.       *expt = -1021;
  64. #if N == 2
  65.       if (res_ptr[1] == 0)
  66.     /* No point in using an extra high-order limb that is zero.  */
  67.     return 1;
  68. #endif
  69.     }
  70.   else
  71.     /* Add the implicit leading one bit for a normalized number.  */
  72.     res_ptr[N - 1] |= 1 << (52 - ((N - 1) * BITS_PER_MP_LIMB));
  73.  
  74.   return N;
  75. }
  76.